/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.fragment;

import lt.marsrutai.mobile.android.activity.intent.FastestRoutesSearchIntentHolder;
import lt.marsrutai.mobile.android.adapter.FastestRouteSearchBookmarkAdapter;
import lt.marsrutai.mobile.android.fragment.task.GetFastestRouteSearchBookmarksTaskFragment;
import lt.marsrutai.mobile.android.fragment.task.TaskFragment;
import lt.marsrutai.mobile.android.model.FastestRouteSearchBookmark;
import lt.marsrutai.mobile.android.task.GetFastestRouteSearchBookmarksTask;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.location.Location;
import android.view.View;
import android.widget.ListView;

/**
 * Fragment which displays fastest route search bookmarks
 * 
 * @author Justas
 */
public class FastestRouteSearchBookmarksFragment extends BookmarksFragment<FastestRouteSearchBookmark> {

    private static final String GET_FASTEST_ROUTE_BOOKMARKS_TASK_FRAGMENT = "GetFastestRouteBookmarksTaskFragment";

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        sendData(getBookmarks().get(position));
    }

    @Override
    protected ListViewAdapter<FastestRouteSearchBookmark> createAdapter() {
        return new FastestRouteSearchBookmarkAdapter(getActivity());
    }

    private void sendData(FastestRouteSearchBookmark bookmark) {
        FastestRoutesSearchIntentHolder intentHolder = new FastestRoutesSearchIntentHolder(getActivity());
        intentHolder.setScopeFolder(bookmark.getScopeFolder());
        intentHolder.setStart(bookmark.getStart());
        
        if (bookmark.getStartLat() != null && bookmark.getStartLng() != null) {
            Location location = new Location((String) null);
            location.setLatitude(bookmark.getStartLat());
            location.setLongitude(bookmark.getStartLng());
            intentHolder.setStartLocation(location);
        }
        intentHolder.setStartSelectionType(bookmark.getStartSelectionType());
        
        intentHolder.setEnd(bookmark.getEnd());
        if (bookmark.getEndLat() != null && bookmark.getEndLng() != null) {
            Location location = new Location((String) null);
            location.setLatitude(bookmark.getEndLat());
            location.setLongitude(bookmark.getEndLng());
            intentHolder.setEndLocation(location);
        }
        intentHolder.setEndSelectionType(bookmark.getEndSelectionType());
        intentHolder.setIsArrivalInd(bookmark.getIsArrivalInd());
        intentHolder.setChangeCount(bookmark.getChangeCount());
        intentHolder.setChangeDuration(bookmark.getChangeDuration());
        intentHolder.setWalkDistance(bookmark.getWalkDistance());
        intentHolder.setWalkSpeed(bookmark.getWalkSpeed());
        intentHolder.setTransportTypes(bookmark.getTransportTypes());
        startActivity(intentHolder.getIntent());
    }

    @Override
    protected String getTaskBroadcastAction() {
        return GetFastestRouteSearchBookmarksTask.BROADCAST_ACTION;
    }

    @Override
    protected Class<? extends TaskFragment<?, ?, ?>> getTaskFragmentClass() {
        return GetFastestRouteSearchBookmarksTaskFragment.class;
    }

    @Override
    protected String getBookmarksTaskFragmentTag() {
        return GET_FASTEST_ROUTE_BOOKMARKS_TASK_FRAGMENT;
    }

}
